package com.example.huwang.activity;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.huwang.R;
import com.example.huwang.dialog.CustomDialog;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;

public class AddBlacklistActivity extends BaseActivity{
    @BindView(R.id.return_btn)
    public ImageView returnBtn;
    @BindView(R.id.return_text)
    public TextView returnText;
    @BindView(R.id.title)
    public TextView title;
    @BindView(R.id.check_phone)
    public RadioButton checkPhone;
    @BindView(R.id.check_net)
    public RadioButton checkNet;
    @BindView(R.id.add_btn)
    public Button addBtn;
    @BindView(R.id.input_content)
    public EditText inputContent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_blacklist);
        //绑定初始化ButterKnife
        ButterKnife.bind(this);
        initview();
    }
    private void initview(){
        title.setText("黑名单");
        if(checkPhone.isChecked()){
            inputContent.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
        returnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        returnText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        checkPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputContent.setInputType(InputType.TYPE_CLASS_NUMBER);
                if(checkPhone.isChecked()){
                    checkPhone.setButtonDrawable(getResources().getDrawable(R.mipmap.check_true));
                    checkNet.setButtonDrawable(getResources().getDrawable(R.mipmap.check_false));
                }else{
                    checkPhone.setButtonDrawable(getResources().getDrawable(R.mipmap.check_false));
                    checkNet.setButtonDrawable(getResources().getDrawable(R.mipmap.check_true));
                }
            }
        });
        checkNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputContent.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                if(checkNet.isChecked()){
                    checkNet.setButtonDrawable(getResources().getDrawable(R.mipmap.check_true));
                    checkPhone.setButtonDrawable(getResources().getDrawable(R.mipmap.check_false));
                }else{
                    checkNet.setButtonDrawable(getResources().getDrawable(R.mipmap.check_false));
                    checkPhone.setButtonDrawable(getResources().getDrawable(R.mipmap.check_true));
                }
            }
        });
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(inputContent.getText().toString().length()<=0){
                    Toast.makeText(AddBlacklistActivity.this, "请先输入内容", Toast.LENGTH_LONG).show();
                    return;
                }
                if(checkPhone.isChecked()){
                    if(isChinaPhoneLegal(inputContent.getText().toString())){
                        CustomDialog customDialog = new CustomDialog(AddBlacklistActivity.this);
                        customDialog.show("");
                    }else{
                        Toast.makeText(AddBlacklistActivity.this, "请输入正确的手机号码", Toast.LENGTH_LONG).show();
                        return;
                    }
                }else if(checkNet.isChecked()){
                    if(inputContent.getText().toString().length()<=0){
                        Toast.makeText(AddBlacklistActivity.this, "网址不能为空", Toast.LENGTH_LONG).show();
                        return;
                    }else{
                        CustomDialog customDialog = new CustomDialog(AddBlacklistActivity.this);
                        customDialog.show("");
                    }
                }
            }
        });
    }

    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        // ^ 匹配输入字符串开始的位置
        // \d 匹配一个或多个数字，其中 \ 要转义，所以是 \\d
        // $ 匹配输入字符串结尾的位置
        String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +
                "|(18[0-9])|(19[8,9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
